package com.object.module.lq.chatroom.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.object.module.lq.chatroom.entity.TGoodFriendEntity;
import com.object.module.lq.chatroom.service.TGoodFriendService;
import com.object.module.lq.chatroom.to.TGoodFriendTo;
import com.object.module.lq.sys.entity.TUserEntity;
import com.object.module.lq.sys.service.TUserService;
import com.object.utils.BeanCopierUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.object.utils.PageUtils;
import com.object.utils.Query;

import com.object.dao.chatroom.TGoodFriendDao;

import javax.annotation.Resource;


@Service("tGoodFriendService")
public class TGoodFriendServiceImpl extends ServiceImpl<TGoodFriendDao, TGoodFriendEntity> implements TGoodFriendService
{

    @Resource
    private TUserService userService;

    @Override
    public PageUtils queryPage(Map<String, Object> params)
    {
        IPage<TGoodFriendEntity> page = this.page(
                new Query<TGoodFriendEntity>().getPage(params),
                new QueryWrapper<TGoodFriendEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 根据id获取全部好友的信息
     * @param examined
     * @return
     */
    @Override
    public List<TGoodFriendTo> getInformationAboutAllYourFriendsBasedOnID(Integer examined)
    {
        List<TGoodFriendEntity> list = list(new LambdaQueryWrapper<TGoodFriendEntity>()
                .eq(TGoodFriendEntity::getTOwn, examined));
        List<Integer> urIds = list.stream().map(i -> i.getTGoodFriend()).collect(Collectors.toList());
        Map<Integer, TGoodFriendEntity> collected = list.stream().collect(Collectors.toMap(TGoodFriendEntity::getTGoodFriend, e -> e));
        //查询用户信息
        List<TUserEntity> tUserEntities = userService.list(new LambdaQueryWrapper<TUserEntity>()
                .select(TUserEntity::getUrAvatar, TUserEntity::getUrEmail, TUserEntity::getUrId)
                .in(TUserEntity::getUrId, urIds));
        //整合好友信息
        List<TGoodFriendTo> goodFriendTos = tUserEntities.stream().map(i ->
        {
            TGoodFriendTo tGoodFriendTo = new TGoodFriendTo();
            BeanCopierUtil.copyObject(i, tGoodFriendTo);
            try {
                String valueByLoginId = StpUtil.getTokenValueByLoginId(i.getUrId());
                tGoodFriendTo.setTGoodFriend(valueByLoginId);
                TGoodFriendEntity friendEntity = collected.getOrDefault(i.getUrId(), null);
                if (friendEntity != null) {
                    tGoodFriendTo.setTGrouping(friendEntity.getTGrouping());
                    tGoodFriendTo.setNickName(friendEntity.getTName());
                }
            } catch (Exception e) {
                //没有上线 就设置为 NotOnline
                tGoodFriendTo.setTGoodFriend("NotOnline");
            }
            return tGoodFriendTo;
        }).collect(Collectors.toList());
        return goodFriendTos;
    }


}
